<?php

class myUser extends sfGuardSecurityUser
{
	public function getAlbum($albumId){
		if($albumId == null)
			return $this->getGuardUser()->getMainAlbum();
		else
			return Doctrine::getTable('Album')->find($albumId);
	}
	
	public function getId(){
		return $this->getGuardUser()->getId();
	}
	
	public function getFirstname(){
		return $this->getGuardUser()->getFirstName();
	}
	
	public function getTaskList($start,$end){
		return $this->getGuardUser()->getTaskList($start,$end);
	}
	
	public function getGroup($group){
		if(is_string($group)){
			$groups = Doctrine::getTable('aclGroup');
			$groups = $groups->findByDql('group_name = ? AND owner = ?', array($group,$this->getId()));
			$group = $groups->getFirst();
		}
		
		if($group instanceof aclGroup)
			return $group;
		else
			return null;
	}
	
	public function getWidgetsPositions($sectionName)
	{
		
		$q = Doctrine_Query::create()
			->select('w.widget_id, u.name, w.row_position, w.column_position, w.minimized')
			->from('WidgetPosition w')
			->leftJoin('w.widget u')
			->leftJoin('u.section s')
			->where('w.user_id = ? and s.name = ?',array($this->getId(),$sectionName));
		$widgets = $q->fetchArray();
		
		$q = Doctrine_Query::create()
			->select('w.widget_id, u.name, w.position')
			->from('SectionWidget w')
			->leftJoin('w.widget u')
			->leftJoin('w.section sw')
			->where('sw.name = ?',$sectionName);

		return array_merge($widgets,$q->fetchArray());
	}

	public function getFriendList(){
		return $this->getGuardUser()->getMainGroup(); 
		//->getUsers();
	}
	
	public function newFriend($user){
		$imYourFriend = new AclUsersGroups();
		$imYourFriend->setGroupId($user->getMainGroupId());
		$imYourFriend->setUserId($this->getId());
		
		$youAreMyFriend = new AclUsersGroups();
		$youAreMyFriend->setGroupId($this->getGuardUser()->getMainGroupId());
		$youAreMyFriend->setUserId($user->getId());
		
		$imYourFriend->save();
		$youAreMyFriend->save();
	}
	
	public function getFriendNews(){
		return $this->getGuardUser()->getFriendNews();
	}	
}
